{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Getting-Started.ipynb",
      "version": "0.3.2",
      "provenance": [],
      "collapsed_sections": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/Eurus-Holmes/PyTorch-Tutorials/blob/master/Getting_Started.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "metadata": {
        "id": "qdU0dcHEjwaF",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Tensors\n",
        "\n",
        "Tensors are similar to NumPy’s ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing."
      ]
    },
    {
      "metadata": {
        "id": "c2ELp6Kji4xU",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        "from __future__ import print_function\n",
        "import torch"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "metadata": {
        "id": "xENo52LAj-sY",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Construct a 5x3 matrix, uninitialized:"
      ]
    },
    {
      "metadata": {
        "id": "8QoQPlTMjbPo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "ab328e65-d713-4409-b53b-d00b493a076b"
      },
      "cell_type": "code",
      "source": [
        "x = torch.empty(5,3)\n",
        "print(x)"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[4.1906e-36, 0.0000e+00, 0.0000e+00],\n",
            "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
            "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
            "        [0.0000e+00, 0.0000e+00, 0.0000e+00],\n",
            "        [0.0000e+00, 0.0000e+00, 0.0000e+00]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "XBDXs6TkkMRJ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Construct a randomly initialized matrix:"
      ]
    },
    {
      "metadata": {
        "id": "If4m6oYfkE-Z",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "09e96818-9804-4d4a-89f6-69a77adb85f2"
      },
      "cell_type": "code",
      "source": [
        "x = torch.rand(5,3)\n",
        "print(x)"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[0.8101, 0.9833, 0.2505],\n",
            "        [0.3211, 0.2849, 0.6044],\n",
            "        [0.8746, 0.3635, 0.6035],\n",
            "        [0.6569, 0.1684, 0.4116],\n",
            "        [0.0088, 0.0886, 0.6449]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Ugn-l9WrkUjm",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Construct a matrix filled zeros and of dtype long:"
      ]
    },
    {
      "metadata": {
        "id": "j-1MYEs4kQ90",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "86ae22f4-a081-44d9-9abd-4d8096663097"
      },
      "cell_type": "code",
      "source": [
        "x = torch.zeros(5,3,dtype=torch.long)\n",
        "print(x)"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[0, 0, 0],\n",
            "        [0, 0, 0],\n",
            "        [0, 0, 0],\n",
            "        [0, 0, 0],\n",
            "        [0, 0, 0]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "JCdxrCvOkkBU",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Construct a tensor directly from data:"
      ]
    },
    {
      "metadata": {
        "id": "S3KVMXIZkdt4",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "e490133b-8340-4d22-8eb1-02e37f5081d5"
      },
      "cell_type": "code",
      "source": [
        "x = torch.tensor([5.5,3])\n",
        "print(x)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([5.5000, 3.0000])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "St3RYdtaktdW",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "or create a tensor based on an existing tensor. These methods will reuse properties of the input tensor, e.g. dtype, unless new values are provided by user"
      ]
    },
    {
      "metadata": {
        "id": "VJ-8ScpikqFV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "5ce6e3d4-85a8-4839-f9e5-a6fa006e9466"
      },
      "cell_type": "code",
      "source": [
        "x = x.new_ones(5,3,dtype=torch.double)\n",
        "print(x)"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[1., 1., 1.],\n",
            "        [1., 1., 1.],\n",
            "        [1., 1., 1.],\n",
            "        [1., 1., 1.],\n",
            "        [1., 1., 1.]], dtype=torch.float64)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "gTdzpJB0k1Aa",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "28d9d0b7-e2b2-49ee-921a-6947f4e3ab01"
      },
      "cell_type": "code",
      "source": [
        "x = torch.randn_like(x, dtype=torch.float)\n",
        "print(x)"
      ],
      "execution_count": 10,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-1.3197,  0.5449,  0.7239],\n",
            "        [ 0.3079,  1.0471, -0.2620],\n",
            "        [ 0.0801,  0.3639,  1.3921],\n",
            "        [-0.9751,  0.2855, -2.0033],\n",
            "        [ 0.9147, -0.4666,  0.8210]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "IbSlRq4UlwGT",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Get its size:"
      ]
    },
    {
      "metadata": {
        "id": "dS9zqw96liTb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "81cf2f32-f2d9-4eaa-f9e3-570c3cbbe6d8"
      },
      "cell_type": "code",
      "source": [
        "print(x.size())"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([5, 3])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "VW5wjS8Vl3Ic",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "\n",
        "\n",
        "### Note:\n",
        "\n",
        "`torch.Size` is in fact a tuple, so it supports all tuple operations.\n",
        "\n"
      ]
    },
    {
      "metadata": {
        "id": "SIkY0i1OmKrj",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# Operations\n",
        "\n",
        "There are multiple syntaxes for operations. In the following example, we will take a look at the addition operation.\n",
        "\n",
        "Addition: syntax 1"
      ]
    },
    {
      "metadata": {
        "id": "Hcpx2ooalyUg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "504d55e9-8ab7-469f-c8d7-291b68e17c68"
      },
      "cell_type": "code",
      "source": [
        "print(x)"
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-1.3197,  0.5449,  0.7239],\n",
            "        [ 0.3079,  1.0471, -0.2620],\n",
            "        [ 0.0801,  0.3639,  1.3921],\n",
            "        [-0.9751,  0.2855, -2.0033],\n",
            "        [ 0.9147, -0.4666,  0.8210]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "zOS0a1BsmcyS",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "f0f6f647-4e6c-4959-e607-f510d92c9b70"
      },
      "cell_type": "code",
      "source": [
        "y = torch.rand(5,3)\n",
        "print(y)"
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[0.6984, 0.6144, 0.1857],\n",
            "        [0.6608, 0.0433, 0.8483],\n",
            "        [0.6670, 0.2028, 0.5131],\n",
            "        [0.1229, 0.4102, 0.4315],\n",
            "        [0.8529, 0.5702, 0.7609]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "38ereMZ6mgtQ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "d4ad5a91-60a7-4e8b-8961-ee3ccb488905"
      },
      "cell_type": "code",
      "source": [
        "print(x+y)"
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.6213,  1.1594,  0.9095],\n",
            "        [ 0.9688,  1.0903,  0.5862],\n",
            "        [ 0.7472,  0.5667,  1.9053],\n",
            "        [-0.8522,  0.6957, -1.5719],\n",
            "        [ 1.7676,  0.1036,  1.5819]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "9mxgNGrUmqIV",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Addition: syntax 2"
      ]
    },
    {
      "metadata": {
        "id": "iBeGZtIgmjR8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "7732fae1-9afd-4f18-a042-273095c4bba8"
      },
      "cell_type": "code",
      "source": [
        "print(torch.add(x,y))"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.6213,  1.1594,  0.9095],\n",
            "        [ 0.9688,  1.0903,  0.5862],\n",
            "        [ 0.7472,  0.5667,  1.9053],\n",
            "        [-0.8522,  0.6957, -1.5719],\n",
            "        [ 1.7676,  0.1036,  1.5819]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "wiPs4PT4mzJW",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Addition: providing an output tensor as argument"
      ]
    },
    {
      "metadata": {
        "id": "f5lYPKpimuKU",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "49513eb7-f664-4947-d9bb-b09ffa2f0ad0"
      },
      "cell_type": "code",
      "source": [
        "result = torch.empty(5,3)\n",
        "torch.add(x,y,out=result)\n",
        "print(result)"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.6213,  1.1594,  0.9095],\n",
            "        [ 0.9688,  1.0903,  0.5862],\n",
            "        [ 0.7472,  0.5667,  1.9053],\n",
            "        [-0.8522,  0.6957, -1.5719],\n",
            "        [ 1.7676,  0.1036,  1.5819]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "q-ZvjFDim_3K",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Addition: in-place"
      ]
    },
    {
      "metadata": {
        "id": "BUtIFwyAm72v",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 102
        },
        "outputId": "6b437741-b127-466d-d596-489195417b9f"
      },
      "cell_type": "code",
      "source": [
        "y.add_(x)\n",
        "print(y)"
      ],
      "execution_count": 17,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.6213,  1.1594,  0.9095],\n",
            "        [ 0.9688,  1.0903,  0.5862],\n",
            "        [ 0.7472,  0.5667,  1.9053],\n",
            "        [-0.8522,  0.6957, -1.5719],\n",
            "        [ 1.7676,  0.1036,  1.5819]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "L9AR1S3hnSWQ",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "### NOTE\n",
        "\n",
        "Any operation that mutates a tensor in-place is post-fixed with an `_`. For example: `x.copy_(y)`, `x.t_()`, will change `x`."
      ]
    },
    {
      "metadata": {
        "id": "rm_iA2Fdnfqw",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "You can use standard NumPy-like indexing with all bells and whistles!"
      ]
    },
    {
      "metadata": {
        "id": "AcDeKNnYnDn9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b1070cb1-4124-4fd3-f6fe-05903204c7d6"
      },
      "cell_type": "code",
      "source": [
        "print(x[:,1])"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([ 0.5449,  1.0471,  0.3639,  0.2855, -0.4666])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "l6LWu5AHntSO",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Resizing: If you want to resize/reshape tensor, you can use `torch.view`:"
      ]
    },
    {
      "metadata": {
        "id": "xkp6y__pnixd",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 85
        },
        "outputId": "03fdd27b-e3a8-40fa-ac2f-4b883dfbe00d"
      },
      "cell_type": "code",
      "source": [
        "x = torch.randn(4,4)\n",
        "print(x)"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.9692,  0.2813, -1.0509,  1.6942],\n",
            "        [ 0.6585,  0.4355,  0.2424, -0.1366],\n",
            "        [ 2.3252, -0.2056,  0.0614, -0.2123],\n",
            "        [-0.1051, -0.5254,  1.6995,  2.6456]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "vqzU2Xivn2MV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "7150b280-3ff7-4d8a-9549-a058c4c74e06"
      },
      "cell_type": "code",
      "source": [
        "y = x.view(16)\n",
        "print(y)"
      ],
      "execution_count": 21,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([-0.9692,  0.2813, -1.0509,  1.6942,  0.6585,  0.4355,  0.2424, -0.1366,\n",
            "         2.3252, -0.2056,  0.0614, -0.2123, -0.1051, -0.5254,  1.6995,  2.6456])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "rKI941QRn6PS",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "bbbfd3bd-0f11-4c7e-aa88-e477be85199c"
      },
      "cell_type": "code",
      "source": [
        "z = x.view(-1,8)\n",
        "print(z)"
      ],
      "execution_count": 22,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[-0.9692,  0.2813, -1.0509,  1.6942,  0.6585,  0.4355,  0.2424, -0.1366],\n",
            "        [ 2.3252, -0.2056,  0.0614, -0.2123, -0.1051, -0.5254,  1.6995,  2.6456]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "Gtle-vvOoFVr",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d5029a58-4d4d-462a-c763-d938c58d0e7a"
      },
      "cell_type": "code",
      "source": [
        "print(x.size(), y.size(), z.size())"
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "0aPaL3lkoYEc",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "If you have a one element tensor, use `.item()` to get the value as a Python number"
      ]
    },
    {
      "metadata": {
        "id": "eSuLmEc8oOk8",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "eb526e9a-b2cd-4023-a895-e8eb37e8f6d6"
      },
      "cell_type": "code",
      "source": [
        "x = torch.randn(1)\n",
        "print(x)\n",
        "print(x.item())"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1.5547])\n",
            "1.5547308921813965\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "7-JCFIFOolcs",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "## Read later:\n",
        "\n",
        "100+ Tensor operations, including transposing, indexing, slicing, mathematical operations, linear algebra, random numbers, etc., are described [here](https://pytorch.org/docs/stable/torch.html)."
      ]
    },
    {
      "metadata": {
        "id": "XcnzRrWqovlV",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# NumPy Bridge\n",
        "\n",
        "Converting a Torch Tensor to a NumPy array and vice versa is a breeze.\n",
        "\n",
        "The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other."
      ]
    },
    {
      "metadata": {
        "id": "S_n7XBTjpFd3",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Converting a Torch Tensor to a NumPy Array"
      ]
    },
    {
      "metadata": {
        "id": "pJBN3b_Woe7a",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c721b209-2ae2-4d20-9af1-ffa26cafd8f8"
      },
      "cell_type": "code",
      "source": [
        "a = torch.ones(5)\n",
        "print(a)"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1., 1., 1., 1., 1.])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "FssaFhDfpMWs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4b62d61e-ec75-4792-f6eb-4d42f2654a33"
      },
      "cell_type": "code",
      "source": [
        "b = a.numpy()\n",
        "print(b)"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1. 1. 1. 1. 1.]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "9thjAW8kpRU1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "134ba232-629f-436c-d912-0d0541c2b4df"
      },
      "cell_type": "code",
      "source": [
        "print(a)"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1., 1., 1., 1., 1.])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "FG7Q210Vpd_U",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "See how the numpy array changed in value."
      ]
    },
    {
      "metadata": {
        "id": "OFPQ2NvgpWqf",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "debdefea-c6e1-4f7e-bb1c-64cb63ccea3d"
      },
      "cell_type": "code",
      "source": [
        "a.add_(1)\n",
        "print(a)"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([2., 2., 2., 2., 2.])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "G0NaPiLyphFl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "8dc02756-2a7e-4099-882e-b773dff697bd"
      },
      "cell_type": "code",
      "source": [
        "print(b)"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[2. 2. 2. 2. 2.]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "e6_Fql8wpnEG",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "Converting NumPy Array to Torch Tensor"
      ]
    },
    {
      "metadata": {
        "id": "MjX_XwcbpqzS",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "See how changing the np array changed the Torch Tensor automatically"
      ]
    },
    {
      "metadata": {
        "id": "rmXxp9VipiWh",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "df91270d-12dd-4560-d977-ab843048fb42"
      },
      "cell_type": "code",
      "source": [
        "import numpy as np\n",
        "a = np.ones(5)\n",
        "print(a)"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[1. 1. 1. 1. 1.]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "OseiMokWp1z5",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2bf39d77-e10b-49a4-85ad-e4abc4bd0554"
      },
      "cell_type": "code",
      "source": [
        "b = torch.from_numpy(a)\n",
        "print(b)"
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([1., 1., 1., 1., 1.], dtype=torch.float64)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "sRIm7lDAp6cs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "eb434a5b-c9e7-4997-a37b-7dec60f11063"
      },
      "cell_type": "code",
      "source": [
        "np.add(a, 1, out=a)"
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([2., 2., 2., 2., 2.])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "metadata": {
        "id": "BdxFyRUgp_KR",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "4a676a61-fa7c-4909-a42b-47538bc92130"
      },
      "cell_type": "code",
      "source": [
        "print(a)"
      ],
      "execution_count": 34,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[2. 2. 2. 2. 2.]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "irpJ7j-gqBYk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "8915ba28-d49b-4490-f1b4-2dea2584fb5b"
      },
      "cell_type": "code",
      "source": [
        "print(b)"
      ],
      "execution_count": 35,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([2., 2., 2., 2., 2.], dtype=torch.float64)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "FsJLI299qJIl",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "All the Tensors on the CPU except a CharTensor support converting to NumPy and back."
      ]
    },
    {
      "metadata": {
        "id": "YD42bct1qPRL",
        "colab_type": "text"
      },
      "cell_type": "markdown",
      "source": [
        "# CUDA Tensors\n",
        "\n",
        "Tensors can be moved onto any device using the `.to` method."
      ]
    },
    {
      "metadata": {
        "id": "pO7aDYbtqCt2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 51
        },
        "outputId": "7725a945-6113-4956-d691-cdb0c559c96b"
      },
      "cell_type": "code",
      "source": [
        "if torch.cuda.is_available():\n",
        "  device = torch.device(\"cuda\")\n",
        "  y = torch.ones_like(x, device=device)\n",
        "  x = x.to(device)\n",
        "  z = x+y\n",
        "  print(z)\n",
        "  print(z.to(\"cpu\",torch.double))"
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([2.5547], device='cuda:0')\n",
            "tensor([2.5547], dtype=torch.float64)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "metadata": {
        "id": "MZlDoy1Jq5EU",
        "colab_type": "code",
        "colab": {}
      },
      "cell_type": "code",
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}